package com.dzsw.mail;


import org.simplejavamail.api.email.Email;
import org.simplejavamail.api.email.EmailPopulatingBuilder;
import org.simplejavamail.api.mailer.Mailer;
import org.simplejavamail.api.mailer.config.TransportStrategy;
import org.simplejavamail.email.EmailBuilder;
import org.simplejavamail.mailer.MailerBuilder;
import org.yaml.snakeyaml.Yaml;

import java.io.BufferedReader;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.nio.file.FileSystems;
import java.nio.file.Files;
import java.nio.file.Path;

public class SimpleJavaMailExample {

    public static void main(String[] args) throws IOException {
//        sendMail0();
        sendMail1();
//        sendMail2();
    }

    private static void sendMail0() {
        Mailer mailer = MailerBuilder
                .withSMTPServer(
                        "smtp.exmail.qq.com",
                        465,
                        "sunzhe@sy-binal.com",
                        "LmUmTJ7bjZdbjRXy"
                )
                .withTransportStrategy(TransportStrategy.SMTPS)
                .buildMailer();
        Email email = EmailBuilder.startingBlank()
                .from("sunzhe@sy-binal.com")
                .to("sunzhe", "sunzhe@sy-binal.com")
                .withSubject("This is a test e-mail, test the Simple Java Mail3.")
                .withPlainText("This is a test e-mail, test the Simple Java Mail3.")
                .buildEmail();
        mailer.sendMail(email);
    }

    private static void sendMail1() {
        Mailer mailer = MailerBuilder
                .withSMTPServer(
                        "192.168.1.67",
                        25,
                        "3860313@qq.com",
                        "LmUmTJ7bjZdbjRXy"
                )
//                .withTransportStrategy(TransportStrategy.SMTPS)
                .buildMailer();
        Email email = EmailBuilder.startingBlank()
                .from("3860313@qq.com")
                .to("sunzhe@sy-binal.com")
                .withSubject("This is a test e-mail, test the Simple Java Mail. 5")
                .withPlainText("This is a test e-mail, test the Simple Java Mail. 6")
                .withAttachment("annex.txt", "this is annex. enen~ ".getBytes(StandardCharsets.UTF_8), "text/plain")
                .buildEmail();
        mailer.sendMail(email);
    }

    private static void sendMail2() throws IOException {
        // read yml
        Yaml yaml = new Yaml();
        ResendMailConfig config = null;
        Path path = FileSystems.getDefault().getPath("D:\\mail.yml");
        try (BufferedReader reader = Files.newBufferedReader(path)) {
            config = yaml.loadAs(reader, ResendMailConfig.class);
            System.out.println(config.toString());
        }

        Mailer mailer = MailerBuilder
                .withSMTPServer(
                        "smtp.exmail.qq.com",
                        465,
                        "sunzhe@sy-binal.com",
                        "LmUmTJ7bjZdbjRXy"
                )
                .withTransportStrategy(TransportStrategy.SMTPS)
                .buildMailer();
        EmailPopulatingBuilder builder = EmailBuilder.startingBlank();
        builder.from("sunzhe@sy-binal.com")
                .to(config.getToList().get(0))
                .withSubject("This is a test e-mail, test the Simple Java Mail.")
                .withPlainText("This is a test e-mail, test the Simple Java Mail.")
                .withAttachment("annex.txt", "this is annex.".getBytes(StandardCharsets.UTF_8), "text/plain");
        config.getToList().stream().skip(1).forEach(builder::cc);
        mailer.sendMail(builder.buildEmail(), true);
    }
}
